package dk.sdu.amaze;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import TUIO.TuioCursor;

/*
 * Describes first game screen - splashscreen
 */
public class StateA extends State {
	//=============================sounds
	Sound logos;
	Thread t_logos;
	//=============================
	private static final long serialVersionUID = 1L;

	public StateA(Amaze context) {
		super(context);
		logos = new Sound("logo.wav", true);
		t_logos = new Thread(logos);
		t_logos.start();
	}

	public void onTap() {
		StateB stateB = new StateB(ctx);
		stateB.selectNextLevel();
		ctx.setState(stateB);
	}

	@Override
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D)g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2.setColor(Color.white);
		g2.fillRect(0, 0, getWidth(), getHeight());

		/* LOGO Section */
		((Graphics2D)g2).setPaint(new GradientPaint(800,600, Color.red, 0,0, Color.green) );
		g2.fillRect(0,0,800,600);

		BufferedImage amazeImage = null;
		try {
				amazeImage = ImageIO.read(new File("amazelogo.png"));
		}
		catch (IOException e) {
			e.printStackTrace();
			System.out.println("Error while loading image.");
		}

		g2.drawImage(amazeImage,200,150,400,300,null);

		/* draw "start" string */
		String startString = "Touch me to start the game!";
		// Find the size of string in font in the current Graphics context g2
		FontMetrics fm   = g2.getFontMetrics(new Font(g2.getFont().getFontName(),Font.BOLD,20));
		java.awt.geom.Rectangle2D rect = fm.getStringBounds(startString, g2);
		int textWidth  = (int)(rect.getWidth());
		g2.setFont(new Font(g2.getFont().getFontName(),Font.BOLD,20));
		g2.setColor(Color.YELLOW);
		g2.drawString(startString, ((800 - textWidth)  / 2), 500);
	}

	@Override
	public void addTuioCursor(TuioCursor tcur) {
		//when I press on any area of this state then I will go on the next state (StateB).
		//=====================sound killer thread
		Sound killer = new Sound(true, "logo.wav");
		Thread t_killer = new Thread(killer);
		t_killer.start();
		onTap();
	}

	@Override
	public void removeTuioCursor(TuioCursor tcur) {
	}

	@Override
	public void updateTuioCursor(TuioCursor tcur) {
	}

}
